<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Search extends Site {

    function setup() {
        $this->run_modes('product', 'event', 'report', 'member', 'clear');
        $this->start_mode('product');
        $this->selfurl = '/search.php';
        parent::setup();
        include("common.php");
    }

    function cgiapp_prerun($rm='') {
        $this->Product = getModel('Model_Product');
        $this->Event = getModel('Model_Event');
        $this->ProductReports = getModel('Model_ProductReports');
        $this->Member = getModel('Model_Member');
        parent::cgiapp_prerun();
    }

    function action_product() {
        $keyword_page = $find = trim($this->get_q('find', ''));
		if($find == '') $keyword_page = 'keyword';
        if ($find == 'keyword')
            $find = '';
        $find = htmlencode($find);
        $page = intval($this->get_q('page', 1, 'int'));
		$ptype = intval($this->get_q('ptype', 0, 'int'));
		$brand = intval($this->get_q('brand', 0, 'int'));
		$pagesize = 12;
		

        $kvcobj = getCache('search_procuct', array('expire' => KVC_EXPIRE));
        $data = $kvcobj->get($find . '_' .$ptype . '_' .$brand . '_' . $page);
        if (!$data) {
            $data = $this->Product->getProductList(0, 0, "TB1.id desc", $page, $pagesize, $find,0,0,0,$ptype,$brand);
            if ($data['result']) {
                foreach ($data['result'] as $k => $v) {
                    $data['result'][$k]['_photo_url'] = PhotoResize($v['cover'], 158, 158);
                    $data['result'][$k]['_difTime'] = explode(" ", str_diff_time($v['apply_endtime']));
                    if (intval($v['is_trial']) == 0) {
                        $data['result'][$k]['_bt'] = '查看產品資料';
                        $data['result'][$k]['_css'] = 'btn_s_v_detail';
                        $data['result'][$k]['_bt_url'] = optimize_url('product', 'detail', 'mtype=' . $v['member_type'] . '&pid=' . $v['id']);
                    } elseif ((strtotime($v['apply_starttime']) <= time() && time() <= strtotime($v['apply_endtime'])) || strtotime($v['apply_starttime']) > time()) {
                        $data['result'][$k]['_bt'] = '立即申請';
                        $data['result'][$k]['_css'] = 'btn_s_apply';
                        $data['result'][$k]['_bt_url'] = optimize_url('product', 'invite_inpage', 'mtype=' . $v['member_type'] . '&pid=' . $v['id']);
                    } elseif (strtotime($v['apply_endtime']) <= time() && time() <= strtotime($v['report_starttime'])) {
                        $data['result'][$k]['_bt'] = '提交報告';
                        $data['result'][$k]['_css'] = 'btn_s_submit';
                        $data['result'][$k]['_bt_url'] = optimize_url('product', 'invite_inpage', 'mtype=' . $v['member_type'] . '&pid=' . $v['id']);
                    } elseif (strtotime($v['report_starttime']) <= time() && time() <= strtotime($v['report_endtime'])) {

                        $data['result'][$k]['_bt'] = '已完結';
                        $data['result'][$k]['_css'] = 'btn_s_com';
                        $data['result'][$k]['_bt_url'] = optimize_url('product', 'detail', 'mtype=' . $v['member_type'] . '&pid=' . $v['id']);
                    } else {
                        $data['result'][$k]['_bt'] = '觀看報告';
                        $data['result'][$k]['_css'] = 'seereport';
                        $data['result'][$k]['_bt_url'] = optimize_url('product', 'detail', 'mtype=' . $v['member_type'] . '&pid=' . $v['id']);
                    }
                }
            }

            $kvcobj->set($find . '_' .$ptype . '_' .$brand . '_' . $page, $data);
        }
        //echo '<pre>'.var_export($data,1).'</pre>'; 	exit;
        $pagelist = showPage($data['pageinfo']['total'], $pagesize, $page, optimize_url('search', 'product', 'find=' . $keyword_page.'&ptype=' . $ptype.'&brand='.$brand));

        $this->search_add_find($find,1);

        $this->tmpl_assign('find', $find);
        $this->tmpl_assign('data', $data);
        $this->tmpl_assign('pagelist', $pagelist);
        $pageValue['title'] = empty($find) ? '全部 - ' : $find . " - ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl('s_product.php');
    }

    function action_event() {
        $pagesize = 5;
        $keyword_page = $find = trim($this->get_q('find', ''));
		if($find == '') $keyword_page = 'keyword';
        if ($find == 'keyword')
            $find = '';
        $find = htmlencode($find);
        $page = intval($this->get_q('page', 1, 'int'));

        $kvcobj = getCache('search_event', array('expire' => KVC_EXPIRE));
        $data = $kvcobj->get($find . '_' . $page);
        if (!$data) {

            $data = $this->Event->getEventList(0, 0, "TB1.id desc", $page, $pagesize, $find);
            //$res = isset($data['result'])&&!empty($data['result'])?$data['result']:array();
            if (!empty($data['result'])) {
                foreach ($data['result'] as $k => $v) {
                    $data['result'][$k]['_difTime'] = explode(" ", str_diff_time($v['endtime']));
                    $data['result'][$k]['_photo_url'] = PhotoResize($v['cover'], 158, 158);

                    //event button url and txt
                    $data['result'][$k]['_button_css'] = "btn_join"; //立即參加
                    $data['result'][$k]['_button_txt'] = "立即參加"; //立即參加
                    $data['result'][$k]['_button_url'] = !empty($v['event_url']) ? $v['event_url'] : optimize_url('events', 'invite_inpage', 'eid=' . $v['id']);
                    $data['result'][$k]['_iscome'] = 1;

                    if (empty($data['result'][$k]['_difTime'][0])) {
                        $data['result'][$k]['_S'] = 1;
                        $data['result'][$k]['_button_css'] = !empty($v['event_url']) ? 'btn_join' : "btn_result";
                        $data['result'][$k]['_button_txt'] = "查看申請結果";
                        $data['result'][$k]['_button_url'] = !empty($v['event_url']) ? $v['event_url'] : optimize_url('events', 'invite_result', 'eid=' . $v['id']);
                    }
                    if (intval($v['has_detail']) > 0 && empty($data['result'][$k]['_difTime'][0])) {
                        $data['result'][$k]['_S'] = 2;
                        $data['result'][$k]['_button_css'] = !empty($v['event_url']) ? 'btn_join' : "btn_l"; //觀看活動報導
                        $data['result'][$k]['_button_txt'] = "觀看活動報導"; //觀看活動報導
                        $data['result'][$k]['_button_url'] = !empty($v['event_url']) ? $v['event_url'] : optimize_url('events', 'detail', 'eid=' . $v['id']);
                    }
                    if (time() < strtotime($v['starttime'])) {
                        $data['result'][$k]['_S'] = 3;
                        $data['result'][$k]['_iscome'] = 0;
                        $data['result'][$k]['_button_css'] = !empty($v['event_url']) ? 'btn_join' : "btn_begin";
                        $data['result'][$k]['_button_txt'] = "即將開始";
                        $data['result'][$k]['_button_url'] = !empty($v['event_url']) ? $v['event_url'] : optimize_url('events', 'invite_inpage', 'eid=' . $v['id']);
                    }
                }
            }
            $kvcobj->set($find . '_' . $page, $data);
        }
        //echo '<pre>'.var_export($data,1).'</pre>'; exit;
        $pagelist = showPage($data['pageinfo']['total'], $pagesize, $page, optimize_url('search', 'event', 'find=' . $keyword_page));

        $this->search_add_find($find,2);

        $this->tmpl_assign('find', $find);
        $this->tmpl_assign('data', $data);
        $this->tmpl_assign('pagelist', $pagelist);
        $pageValue['title'] = empty($find) ? '全部 - ' : $find . " - ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl('s_event.php');
    }

    function action_report() {
        $pagesize = 5;
        $keyword_page = $find = trim($this->get_q('find', ''));
		if($find == '') $keyword_page = 'keyword';
        if ($find == 'keyword')
            $find = '';
        $page = intval($this->get_q('page', 1, 'int'));

        $kvcobj = getCache('search_report', array('expire' => KVC_EXPIRE));
        $data = $kvcobj->get($find . '_' . $page);
        if (!$data) {

            $data = $this->ProductReports->getReportsList(0, 0, 0, "TB1.id desc", $page, $pagesize, 1, $find);
            if ($data['result']) {
                foreach ($data['result'] as $k => $v) {
                    $data['result'][$k]['product_info'] = $this->Product->getProductById($v['oid']);
                }
            }
            $reps = $_temp = array();
            if ($data['result']) {
                foreach ($data['result'] as $k => $v) {
                    $reps[$k]['product']['id'] = $v['oid'];
                    $reps[$k]['product']['name'] = $v['product_info']['name'];
                    $reps[$k]['product']['cname'] = $v['product_info']['cname'];
                    $reps[$k]['product']['type_name'] = $v['product_info']['type_name'];
                    $reps[$k]['product']['photo'] = PhotoResize($v['product_info']['cover'], 116, 116);
                    $reps[$k]['product']['member_type'] = $v['product_info']['member_type'];
                    $reps[$k]['product']['good'] = $v['product_info']['product_reports_stat']['good'];
                    $reps[$k]['product']['common'] = $v['product_info']['product_reports_stat']['common'];
                    $reps[$k]['product']['bad'] = $v['product_info']['product_reports_stat']['bad'];

                    $reps[$k]['report']['id'] = $v['id'];
                    $reps[$k]['report']['title'] = $v['title'];
                    $reps[$k]['report']['body'] = wordscut($v['body'], 100);
                    $reps[$k]['report']['likes'] = $v['likes'];
                    if ($v['product_info']['vote_option_data']['result']) {
                        foreach ($v['product_info']['vote_option_data']['result'] as $k1 => $v1) {
                            $reps[$k]['report']['category'][$k1]['k'] = $k1;
                            $reps[$k]['report']['category'][$k1]['name'] = $v1['cname'];
                            $reps[$k]['report']['category'][$k1]['value'] = $v['category_score' . ($k1 + 1)];
                        }
                    }


                    $_temp = json_decode($v['content_json'], true);
                    if ($_temp) {
                        foreach ($_temp as $k2 => $v2) {
                            $reps[$k]['report']['content'][$k2]['p'] = PhotoResize($v2['p'], 163, 102);
                            $reps[$k]['report']['content'][$k2]['t'] = $v2['t'];
                        }
                    }
                }
            }
            $kvcobj->set($find . '_' . $page, $data);
        }

        $pagelist = showPage($data['pageinfo']['total'], $pagesize, $page, optimize_url('search', 'report', 'find=' . $keyword_page));
        //echo '<pre>'.var_export($data,1).'</pre>';

        $this->tmpl_assign('total', $data['pageinfo']['total']);
        $this->tmpl_assign('find', str_replace('"', '&#34;', str_replace("'", '&#39;', $find)));
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('reps', $reps);
        $pageValue['title'] = empty($find) ? '全部 - ' : $find . " - ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl("s_report.php");
    }

    function action_member() {
        $pagesize = 20;
        $keyword_page = $find = trim($this->get_q('find', ''));
		if($find == '') $keyword_page = 'keyword';
        if ($find == 'keyword')
            $find = '';
        $find = htmlencode($find);
        $page = $this->get_q('page', 1, 'int');

        $kvcobj = getCache('search_member', array('expire' => KVC_EXPIRE));
        $data = $kvcobj->get($find . '_' . $page);
        if (!$data) {

            $data = $this->Member->searchMember($find, "id desc", $page, $pagesize);
            foreach ($data['result'] as $k => $v) {
                $data['result'][$k]['_photo_url'] = imore_avatar($v['photo']);
            }
            $kvcobj->set($find . '_' . $page, $data);
        }
        //echo '<pre>'.var_export($data,1).'</pre>'; exit;
        $pagelist = showPage($data['pageinfo']['total'], $pagesize, $page, optimize_url('search', 'member', 'find=' . $keyword_page));

        $this->search_add_find($find,3);

        $this->tmpl_assign('find', $find);
        $this->tmpl_assign('data', $data);
        $this->tmpl_assign('pagelist', $pagelist);
        $pageValue['title'] = empty($find) ? '全部 - ' : $find . " - ";
        $this->tmpl_assign('pageValue', $pageValue);
        return $this->load_tmpl('s_member.php');
    }

    function action_clear() {
        $kvc = trim($this->get_q('kvc', ''));
        clear_kvcache($kvc);
    }

    function search_add_find($find="",$type=0) {
        if (!empty($find)) {
            $c = db_query_count("keywords", "name='".mysql_escape_string($find)."' and type=".mysql_escape_string($type));
            if(intval($c)>0){
                db_handle("update keywords set times=times+1 where name='".mysql_escape_string($find)."' and type=".mysql_escape_string($type));
            }else{
                db_handle("insert into keywords set name='".mysql_escape_string($find)."',type=".mysql_escape_string($type).",times=1;");
            }

        }
    }

}

$webapp = new Search();
$webapp->run();
?>